package com.hzjh.h5mall.ui.wallet

import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.R
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.ext.widget.balloon.CommonTipBalloon
import com.hzjh.base.frame.adapter.MutableAdapter
import com.hzjh.base.style.weight.ToastKt.toastCenter
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.h5mall.adapter.wallet.WalletProcessVH
import com.hzjh.h5mall.databinding.ActivityWalletDetailBinding
import com.hzjh.h5mall.databinding.ItemWalletProcessBinding
import com.hzjh.h5mall.model.wallet.WalletVM
import com.skydoves.balloon.balloon

//充值和提现详情
@Route(path = RouteUrl.WALLET_DETAIL_ACTIVITY)
class WalletDetailActivity : JHBaseActivity<ActivityWalletDetailBinding,WalletVM>() {
    @JvmField
    @Autowired(name = RouteUrl.PARAMS_WALLET_ID)
    var id = ""

    private val balloon by balloon<CommonTipBalloon>()

    private val processAdapter by lazy {
        MutableAdapter(viewModel.processList.value!!).apply {
            addVhDelegate {
                WalletProcessVH(ItemWalletProcessBinding.inflate(layoutInflater, it, false),
                    clickFailed ={view,message->
                        //弹气泡提示
                        balloon.showAlignBottom(view)
                        balloon.getContentView().apply {
                            findViewById<TextView>(R.id.tv_message).text = message
                        }
                    }){position ->
                    //是否最后一个item
                    position != viewModel.processList.value!!.size-1
                }
            }
        }
    }

    override fun initData() {
        viewModel.reqWalletDetail(id)
    }

    override fun initView() {
        viewBinding.rvProcess.layoutManager = LinearLayoutManager(this)
        viewBinding.rvProcess.adapter=processAdapter
        viewBinding.tvTypeValue.setOnClickListener {
            //支持复制
            CommUtils.copyText(viewBinding.tvTypeValue.text.toString())
            toastCenter(CommUtils.getString(R.string.jh_copy_succeed))
        }
    }

    override fun initObservable() {
        super.initObservable()
        viewModel.walletDetailBean.observe(this){
            var title=""
            when(it.pm){
                0->{
                    //提现
                    title=CommUtils.getString(R.string.jh_withdrawal)
                    viewBinding.tvTypeName.text=CommUtils.getString(R.string.jh_withdrawal_to)
                    viewBinding.tvTypeValue.text=it.linkId
                }
                1->{
                    //充值
                    title=CommUtils.getString(R.string.jh_recharge)
                    viewBinding.tvTypeName.text=CommUtils.getString(R.string.jh_trading_code)
                    viewBinding.tvTypeValue.text=it.linkId
                }
            }
            //0 = 待确定 1 = 有效 -1 = 无效
            title = when(it.status){
                0,1->{
                    viewBinding.ivStatusTag.setImageResource(R.mipmap.icon_success)
                    "$title ${CommUtils.getString(R.string.jh_Progress)}"
                }else->{
                    viewBinding.ivStatusTag.setImageResource(R.mipmap.icon_failed)
                    "$title ${CommUtils.getString(R.string.jh_failed)}"
                }
            }
            viewBinding.tvTitle.text=title
            viewBinding.tvOrderMoney.text=it.number
            viewBinding.tvCreateTime.text=it.createTime
            viewBinding.tvOrderId.text=it.id
            viewModel.getProcessList(it)

        }
        viewModel.processList.observe(this){
            processAdapter.notifyDataSetChanged()
        }
    }

    override fun initViewBinding(): ActivityWalletDetailBinding {
        return ActivityWalletDetailBinding.inflate(layoutInflater)
    }
}